package com.zhl.config;

import feign.Feign;
import feign.Target;
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
import org.springframework.cloud.openfeign.FeignCircuitBreaker;
import org.springframework.cloud.openfeign.FeignClientFactoryBean;
import org.springframework.cloud.openfeign.FeignContext;
import org.springframework.cloud.openfeign.Targeter;
import org.springframework.web.servlet.DispatcherServlet;


public class LocalFeignTargeter implements Targeter {
    private final DispatcherServlet dispatcherServlet;
    public LocalFeignTargeter(DispatcherServlet dispatcherServlet) {
        this.dispatcherServlet = dispatcherServlet;
    }

    @Override
    public <T> T target(FeignClientFactoryBean factory, Feign.Builder feign, FeignContext context, Target.HardCodedTarget<T> target) {
        if (!(feign instanceof FeignLocal.Builder)) {
            return feign.target(target);
        }

        FeignLocal.Builder builder = (FeignLocal.Builder) feign;

        return builder(builder).target(target);
    }

    private FeignLocal.Builder builder(FeignLocal.Builder builder) {
        // 留个扩展

        return builder.dispatcherServlet(dispatcherServlet);
    }
}
